package com.usian.order.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.usian.common.Result;
import com.usian.common.ResultCodeEnum;
import com.usian.entity.Cars;
import com.usian.entity.Favorites;
import com.usian.order.feign.CarFeignClient;
import com.usian.order.mapper.FavoritesMapper;
import com.usian.order.service.IFavoritesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.vo.CollectionDetailsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2025-01-15
 */
@Service
public class FavoritesServiceImpl extends ServiceImpl<FavoritesMapper, Favorites> implements IFavoritesService {

    @Autowired
    private CarFeignClient carFeignClient;




/**
 * 获取用户收藏详情
 *
 * @param userId 用户ID，用于查询该用户的收藏信息
 * @return 返回收藏详情列表，包含车辆相关信息
 */
@Override
public Result<CollectionDetailsVo> CollectionDetails(Integer userId) {
    // 创建查询包装器，用于查询指定用户的收藏信息
    LambdaQueryWrapper<Favorites> wrapper = new LambdaQueryWrapper<>();
    // 查询用户收藏的车辆信息
    List<Favorites> favoritesList = this.list(wrapper.eq(Favorites::getUserId, userId));
    // 如果收藏列表为空，返回数据不存在的结果
//    if (favoritesList.isEmpty()){
//        return Result.build(null, ResultCodeEnum.DATA_NOT_EXISTS);
//    }
    // 创建收藏详情列表，用于存储查询到的车辆详细信息
    List<CollectionDetailsVo> collectionDetailsVos = new ArrayList<>();
    // 遍历收藏列表，查询每辆车的详细信息
    for (Favorites favorites : favoritesList) {
        // 调用远程服务，根据车辆ID获取车辆图片
        Result<String> carImagesByCarId = carFeignClient.getCarImagesByCarId(favorites.getCarId());
        // 调用远程服务，根据车辆ID获取车辆信息
        Result<Cars> carByCarId = carFeignClient.getCarByCarId(favorites.getCarId());
        // 创建收藏详情对象，用于存储单个车辆的详细信息
        CollectionDetailsVo collectionDetailsVo = new CollectionDetailsVo();
        // 设置车辆图片URL
        collectionDetailsVo.setImageUrl(carImagesByCarId.getData());
        // 设置车辆名称
        collectionDetailsVo.setCarName(carByCarId.getData().getCarModel());
        // 设置油箱剩余油量
        collectionDetailsVo.setFuelLevel(String.valueOf(carByCarId.getData().getFuelLevel()));
        // 设置每小时租金
        collectionDetailsVo.setPricePerHour(carByCarId.getData().getPricePerHour());
        // 设置车辆评分
        collectionDetailsVo.setRating(carByCarId.getData().getRating());
        collectionDetailsVo.setCarId(carByCarId.getData().getCarId());
        collectionDetailsVo.setIsFavorite(carByCarId.getData().getIsFavorite());
        // 将车辆详细信息添加到收藏详情列表中
        collectionDetailsVos.add(collectionDetailsVo);
    }
    // 打印收藏详情列表，用于调试目的
    System.out.println(collectionDetailsVos.toString());
    // 返回收藏详情列表和成功结果码
    return Result.build(collectionDetailsVos, ResultCodeEnum.SUCCESS);
}




    @Override
    public Boolean gatFavoritesAll(Integer userId) {
        carFeignClient.updateFavorites();
        LambdaQueryWrapper<Favorites> wrapper = new LambdaQueryWrapper<>();
        List<Favorites> favoritesList = this.list(wrapper.eq(Favorites::getUserId, userId));
        if (favoritesList.isEmpty()){
            carFeignClient.updateFavorites();
        }else {
            for (Favorites favorites : favoritesList) {
                carFeignClient.updateFavoritesAll(favorites.getCarId());

            }
        }
        return true;
    }

}
